package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.RecommendDao;
import com.example.domain.Recommend;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.RecommendService;
import com.example.tool.DateTool;

@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    RecommendDao recommendDao;

    /**
     * @describe 添加退班信息
     */
    @Override
    public Result addRecommend(Recommend recommend) {
        if (recommend != null) {
            // 获取系统当前时间作为记录创建时间
            recommend.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        Recommend recommend1 = recommendDao.save(recommend);

        if (recommend1 != null) {
            return ResultFactory.buildSuccessResult(recommend1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新就业推荐信息
     */
    @Override
    public Result updateRecommend(Recommend recommend) {
        if (recommend != null) {
            // 获取系统当前时间作为记录更新时间
            recommend.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        Recommend recommend1 = recommendDao.saveAndFlush(recommend);

        if (recommend1 != null) {
            return ResultFactory.buildSuccessResult(recommend1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据就业推荐编号删除就业推荐信息
     */
    @Override
    public Result deleteRecommendById(String recommendId) {
        int row = recommendDao.deleteByRecommendId(Integer.parseInt(recommendId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有就业推荐信息
     */
    @Override
    public Result queryAllRecommend() {
        List<Recommend> list = recommendDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询就业推荐信息
     */
    @Override
    public Result queryRecommendById(String recommendId) {
        List<Recommend> list = recommendDao.findByRecommendId(Integer.parseInt(recommendId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加就业推荐信息
     */
    @Override
    public Result addRecommendByExcel(List<Recommend> list) {
        List<Recommend> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = recommendDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据就业推荐编号批量删除就业推荐信息
     */
    @Override
    public Result deleteRecommendByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = recommendDao.deleteByRecommendIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 就业推荐信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
        if (info[0].equals("enterpriseName")) {

            list = recommendDao.findByEnterpriseNameLike(info[1]);

        } else if (info[0].equals("enterpriseTelphone")) {

            list = recommendDao.findByEnterpriseTelphoneLike(info[1]);

        } else if (info[0].equals("referrerName")) {

            list = recommendDao.findByReferrerNameLike(info[1]);

        } else if (info[0].equals("status")) {

            list = recommendDao.findByStatusLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = recommendDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = recommendDao.findByNickNameLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有推荐以及教师信息
     */
    @Override
    public Result queryAllRecommendAndUsersInfo() {
        // TODO 查询所有推荐以及教师信息
        List<Map<String, Object>> list = recommendDao.queryAllRecommendAndUsersInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
